<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_jianghu/model')->方法名();
*/
namespace jy_jianghu;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function checkbang(){
		global $_AR;
		if(!$_SESSION['uid']){
			$_SESSION['forward_url']=$_AR['access_json'];
			$this->model('member')->checklogin();
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','nickname','avatar'));
			if(empty($member)){
				model('core/member')->logout();
				model('core/core')->checklogin('jy_jianghu/home/');
			}else{
				$bangpai=pdo_get('jy_jianghu_bangpai',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
				if(!$bangpai['id']){
					$insert=array(
						'mid'=>$_SESSION['uid'],
						'title'=>$member['nickname'],
						'image'=>$member['avatar'],
						'uniacid'=>$_SESSION['uniacid'],
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_jianghu_bangpai',$insert);
					$bangid=pdo_insertid();
					$insert=array(
						'mid'=>$_SESSION['uid'],
						'jianghu_id'=>$bangid,
						'uniacid'=>$_SESSION['uniacid'],
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_jianghu_myjia',$insert);
				}
			}
		}
	}
	//退出帮派
	public function tuibang($bangpaiid){
		$bangpai=pdo_get('jy_jianghu_bangpai',array('id'=>$bangpaiid,'uniacid'=>$_SESSION['uniacid']));
		$isjia=pdo_count('jy_jianghu_myjia',array('mid'=>$_SESSION['uid'],'jianghu_id'=>$bangpai['id'],'uniacid'=>$_SESSION['uniacid']),'mid');
		if($isjia){
			pdo_delete('jy_jianghu_myjia',array('mid'=>$_SESSION['uid'],'jianghu_id'=>$bangpai['id'],'uniacid'=>$_SESSION['uniacid']));
			$params=array(
				'msg'=>'退出<'.$bangpai['title'].'>帮派',
				'jianghu_id'=>$bangpai['id'],
				'status'=>1,
				'mid'=>$_SESSION['uid'],
			);
			$this->model('model')->set_log($params,3);
			return 1;
		}
		return 0;
	}
	public function checkjiabang($bangpai){
		if(!$_SESSION['uid']){
			return 5;
		}
		$isjia=pdo_count('jy_jianghu_myjia',array('mid'=>$_SESSION['uid'],'jianghu_id'=>$bangpai['id'],'uniacid'=>$_SESSION['uniacid']),'mid');
		if(!$isjia){
			$basic=$this->plugin_setting('basic');
			$isjianum=pdo_count('jy_jianghu_myjia',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'mid');
			$basic['max_jiaru']=$basic['max_jiaru']?:81;
			if($basic['max_jiaru'] && $basic['max_jiaru']<=$isjianum){
				return 2;
			}
			$isnum=pdo_count('jy_jianghu_myjia',array('jianghu_id'=>$bangpai['id']),'mid');
			$basic['max_num']=$basic['max_num']?:108;
			if($basic['max_num'] && $basic['max_num']<=$isnum){
				return 3;
			}
			$insert=array(
					'mid'=>$_SESSION['uid'],
					'jianghu_id'=>$bangpai['id'],
					'uniacid'=>$_SESSION['uniacid'],
					'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_jianghu_myjia',$insert);
			$params=array(
				'msg'=>'加入<'.$bangpai['title'].'>帮派',
				'jianghu_id'=>$bangpai['id'],
				'status'=>1,
				'mid'=>$_SESSION['uid'],
			);
			$this->model('model')->set_log($params,2);
			$this->model('member')->sendbang_neili($bangpai);
			return 1;
		}
		return 0;
	}
	public function getzhanli($bangpai){
		if($bangpai['uptime']>TIMESTAMP-10){
			return array();
		}
		$basic=$this->plugin_setting('basic');
		$myjias=pdo_getall('jy_jianghu_myjia',array('jianghu_id'=>$bangpai['id'],'uniacid'=>$_SESSION['uniacid']),array('mid'));
		$level_zhanli=0;
		$gaogroup_num=0;
		$zhonggroup_num=0;
		$digroup_num=0;
		$orderfee_li=0;
		$all_fee=0;
		$all_ordernum=0;
		foreach($myjias as $item){
			//消费额
			$orderfee=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'price');
			$all_fee +=$orderfee;
			$basic['zhanpayfee_li']=$basic['zhanpayfee_li']?:1;
			$basic['zhanpayfee']=$basic['zhanpayfee']?:1;
			if($basic['zhanpayfee'] && $basic['zhanpayfee_li']){
				$orderfee_li=intval($basic['zhanpayfee_li']*($orderfee/$basic['zhanpayfee']));
			}
			$level=pdo_getcolumn('jy_weishop_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'level');
			//高级长老绑定等级
			if($basic['gaogroup_id'] && $basic['gaogroup_id']==$level){
				$level_zhanli +=$basic['gaogroup_li']?:1;
				$gaogroup_num++;
			}elseif($basic['zhonggroup_id'] && $basic['zhonggroup_id']==$level){
				$level_zhanli +=$basic['zhonggroup_li']?:1;
				$zhonggroup_num++;
			}elseif($basic['digroup_id'] && $basic['digroup_id']==$level){
				$level_zhanli +=$basic['digroup_li']?:1;
				$digroup_num++;
			}else{
				$level_zhanli +=$basic['group_li']?:1;
			}
		}
		$data=array(
			'level_zhanli'=>$level_zhanli,
			'gaogroup_num'=>$gaogroup_num,
			'zhonggroup_num'=>$zhonggroup_num,
			'digroup_num'=>$digroup_num,
			'orderfee_li'=>$orderfee_li,
			'all_li'=>$orderfee_li+$level_zhanli,
			'all_fee'=>$all_fee,
			'all_ordernum'=>$all_ordernum,
			'all_num'=>count($myjias)
		);
		pdo_update('jy_jianghu_bangpai',array('zhanli'=>$data['all_li'],'uptime'=>TIMESTAMP),array('id'=>$bangpai['id']));
		return $data;
	}
	/*
		日志信息
	*/
	public function set_log($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'jianghu_id'=>$params['jianghu_id'],
			'msg'=>$params['msg'],
			'status'=>$params['status'],
			'type'=>$type,
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_jianghu_log',$insert);
		return true;
	}
}
?>